<?php

class DefaultViewAction extends FrontendAction
{

    public function __construct()
    {
        parent::__construct();
        $this->responseType = 'tpl';
        $this->layout = 'index';
    }

    public function execute()
    {
        $id = $this->context->get('id');
        if (!is_numeric($id))
        {
            $id = base64_decode($id);
            $tmp = explode('_', $id);
            $id = $tmp[0];
        }
        //�����ؼ��
        $searchKeyService = new SearchKeyService();
        $searchKey = $this->getSearchKeyword();

        if ($searchKey != '' && strpos($searchKey, 'baidu.com') == false)
        {
            $searchKeyService->addOrUpdate($id, $searchKey);
            $this->data['config']['search8']['keyword'] = $searchKey;
        }

        $productService = C('ProductService');
        $info = $productService->getProductById($id);
        if ($info)
        {
            if (strpos($info['item']['title'], '得一健') !== false)
            {
                header('location:/mall/');
            }
//var_dump($info);die;
            $info['click_url'] = str_replace('mm_17538449_0_0', 'xiazaiquanid', $info['click_url']);
            $info['shop_click_url'] = str_replace('mm_17538449_0_0', 'xiazaiquanid', $info['shop_click_url']);
            if (!$this->data['isRobot'] && $searchKey != '' && strpos($searchKey, 'baidu.com') == false)
            {
                if ($info['click_url'] != '')
                {
                    //header('Location:'.$info['click_url']);
                    //return;
                }
            }

            //�滻����
            $info['item']['desc'] = str_replace('taobao.net', 'taobaocdn.com', $info['item']['desc']);

            if (preg_match_all('~(http://img\d+\.taobaocdn\.com[^\'">\s]*)([\'">\s])~is', $info['item']['desc'], $m))
            {
                $num = count($m[1]);
                for ($ii = 0; $ii < $num; $ii++)
                {
                    $info['item']['desc'] = str_replace($m[1][$ii] . $m[2][$ii], $this->data['baseurl'] . 'images/loading.gif' . $m[2][$ii] . ' u="' . $this->data['cf']->getEncodeImg($m[1][$ii]) . '"', $info['item']['desc']);
                }
            }

            $tblink=str_replace('http://s.click.taobao.com/', 'xiazaiquanmall', $info['click_url']);                        
            $info['item']['desc'] = preg_replace('~<a.*?>~is', '<a rel="nofollow" href="#" nid="' . $id . '" target="_blank">', $info['item']['desc']);
            $info['item']['desc'] = preg_replace('~<embed .*?>~is', '', $info['item']['desc']);
            $info['item']['desc'] = str_replace('alimama', urlencode('alimama'), $info['item']['desc']);
            $info['item']['desc'] = str_replace('taobao', urlencode('taobao'), $info['item']['desc']);

            $categoryName = '';
            if ($info['item']['cid'] > 0)
            {
                $categoryService = C('CategoryService');
                $parentCategoryList = $categoryService->getParentList($info['item']['cid']);
                if ($parentCategoryList)
                {
                    $this->data['parentCategoryList'] = $parentCategoryList;
                    $categoryName = $parentCategoryList[count($parentCategoryList) - 1]['name'];
                    if (count($parentCategoryList) >= 2)
                    {
                        $this->data['parentCategoryId'] = $parentCategoryList[count($parentCategoryList) - 2]['id'];
                    }
                    //��Ŀ������
                    if ($parentCategoryList[count($parentCategoryList) - 1]['is_hide'] == 1)
                    {
                        $this->layout = 'result';
                        $this->code = 403;
                        return;
                    }
                }
            }

            //$seoConfig = C('SettingService')->get('seo_setting', true);
            // $seoConfig='a:17:{s:4:"mode";s:7:"rewrite";s:7:"postfix";s:4:".htm";s:12:"listshortcut";s:4:"list";s:14:"detailshortcut";s:4:"view";s:16:"postlistshortcut";s:5:"forum";s:18:"postdetailshortcut";s:6:"thread";s:19:"pindaoindexshortcut";s:6:"pindao";s:17:"pageindexshortcut";s:7:"zhuanti";s:10:"indextitle";s:38:"正品商城,特卖商城,网上商城";s:12:"indexkeyword";s:38:"正品商城|特卖商城|网上商城";s:16:"indexdescription";s:69:"正品商城,精选网上商城正品及特卖产品,支付宝购买.";s:9:"listtitle";s:36:"商城正品{catName}{keyword}{page}";s:11:"listkeyword";s:47:"商城{catName}|正品{catName}|热卖{catName}";s:15:"listdescription";s:43:"网上商城2011年热卖的正品{catName}";s:11:"detailtitle";s:19:"商城正品{title}";s:13:"detailkeyword";s:7:"{title}";s:17:"detaildescription";s:25:"商城正品特卖{title}";}';
            //$seoConfig=unserialize($seoConfig);
            //修改记录 2011.3.27 取消seo设置查库
            $seoConfig = array();
            $seoConfig['detailtitle'] = '商城正品{title}';
            $seoConfig['detailkeyword'] = '{title}';
            $seoConfig['detaildescription'] = '商城正品特卖{title}';

            $seoTitle = $this->getSeoStr($seoConfig['detailtitle'], array('title' => $info['item']['title'], 'catName' => $categoryName, 'nick' => $info['item']['nick'], 'price' => $info['item']['price']));
            $seoKeyword = $this->getSeoStr($seoConfig['detailkeyword'], array('title' => $info['item']['title'], 'catName' => $categoryName, 'nick' => $info['item']['nick'], 'price' => $info['item']['price']));
            $seoDescription = $this->getSeoStr($seoConfig['detaildescription'], array('title' => $info['item']['title'], 'catName' => $categoryName, 'nick' => $info['item']['nick'], 'price' => $info['item']['price']));

            $this->data['seo']['title'] = $seoTitle;
            $this->data['seo']['keyword'] = $seoKeyword;
            $this->data['seo']['description'] = $seoDescription;

            $this->data['product'] = $info;
        }

        //修改记录 2011.3.28
        if ($this->data['isRobot'] && $searchKey == '')
        {
            $searchKeyList = $searchKeyService->getListByNumIid($id);
            if (empty($searchKeyList))
            {
                $searchKeyList = array();
            }
            //�ȴ�
            $hotkeyList = array();

            if ($info && $info['cid'] > 0)
            {
                $hotkeyService = new HotkeyService();
                $hotkeyList = $hotkeyService->getListByCategoryId($info['cid']);

                if (empty($hotkeyList))
                {
                    $hotkeyList = array();
                }

                if (count($hotkeyList) > 20)
                {
                    array_splice($hotkeyList, 18);
                }
            }

            $this->data['keywordList'] = array_merge($searchKeyList, $hotkeyList);
        }
        //###############################################

        if (!$this->data['isRobot'])
        {
            if (time() % 100 == 0)
            {
                $num = rand();
                if ($num % 20 == 0)
                {
                    C('ViewHistoryService')->updateViewHistory($id, 1);
                }
            }
        }

        $this->data['currentUrl'] = 'http://' . $this->data['domain'] . C('LtUrl')->generate('Default', 'View', array('id' => $this->context->get('id')));
    }

}
